<?php

     $eWAY_CustomerID = $_POST['cid'];    // Set this to your eWAY Customer ID
     $eWAY_PaymentMethod = "".$_POST['emethod']."";  // Set this to the payment gatway you would like to use (REAL_TIME, REAL_TIME_CVN or GEO_IP_ANTI_FRAUD)

	if($_POST['elive'] =="false"){
	$eWAY_UseLive = false; // Set this to true to use the live gateway
	}else{
	$eWAY_UseLive = true;
	}

	//define default values for eway
	define('EWAY_DEFAULT_CUSTOMER_ID',$_POST['cid']);
	define('EWAY_DEFAULT_PAYMENT_METHOD', $eWAY_PaymentMethod); // possible values are: REAL_TIME, REAL_TIME_CVN, GEO_IP_ANTI_FRAUD
	define('EWAY_DEFAULT_LIVE_GATEWAY', $eWAY_UseLive); //<false> sets to testing mode, <true> to live mode

    //define script constants
	define('REAL_TIME', 'REAL-TIME');
	define('REAL_TIME_CVN', 'REAL-TIME-CVN');
	define('GEO_IP_ANTI_FRAUD', 'GEO-IP-ANTI-FRAUD');

    //define URLs for payment gateway

	define('EWAY_PAYMENT_LIVE_REAL_TIME', 'https://www.eway.com.au/gateway/xmlpayment.asp');
	define('EWAY_PAYMENT_LIVE_REAL_TIME_TESTING_MODE', 'https://www.eway.com.au/gateway/xmltest/testpage.asp');
	define('EWAY_PAYMENT_LIVE_REAL_TIME_CVN', 'https://www.eway.com.au/gateway_cvn/xmlpayment.asp');
	define('EWAY_PAYMENT_LIVE_REAL_TIME_CVN_TESTING_MODE', 'https://www.eway.com.au/gateway_cvn/xmltest/testpage.asp');
	define('EWAY_PAYMENT_LIVE_GEO_IP_ANTI_FRAUD', 'https://www.eway.com.au/gateway_cvn/xmlbeagle.asp');
	define('EWAY_PAYMENT_LIVE_GEO_IP_ANTI_FRAUD_TESTING_MODE', 'https://www.eway.com.au/gateway_cvn/xmlbeagle.asp'); //in testing mode process with REAL-TIME
	define('EWAY_PAYMENT_HOSTED_REAL_TIME', 'https://www.eway.com.au/gateway/payment.asp');
	define('EWAY_PAYMENT_HOSTED_REAL_TIME_TESTING_MODE', 'https://www.eway.com.au/gateway/payment.asp');
	define('EWAY_PAYMENT_HOSTED_REAL_TIME_CVN', 'https://www.eway.com.au/gateway_cvn/payment.asp');
	define('EWAY_PAYMENT_HOSTED_REAL_TIME_CVN_TESTING_MODE', 'https://www.eway.com.au/gateway_cvn/payment.asp');

require_once('EwayPaymentLive.php');
if (isset($_POST['btnProcess'])){
 
  $cartError = "";
  $txtFirstName 	= $_POST['txtFirstName'];
  $txtLastName 		= $_POST['txtLastName'];
  $txtEmail 		= $_POST['txtEmail'];
  $txtAddress 		= $_POST['txtAddress'];
  $txtPostcode 		= $_POST['txtPostcode'];
  $txtTxnNumber 	= $_POST['txtTxnNumber'];
  $txtInvDesc 		= $_POST['txtInvDesc'];
  $txtInvRef 		= $_POST['txtInvRef'];
  $txtOption1 		= "";
  $txtOption2 		= "";
  $txtOption3 		= "";
  $txtCCNumber 		= $_POST['txtCCNumber'];
  $ddlExpiryMonth 	= $_POST['ddlExpiryMonth'];
  $ddlExpiryYear 	= $_POST['ddlExpiryYear'];
  $txtCCName 		= $_POST['txtCCName'];
  $txtAmount 		= $_POST['txtAmount'];

  
  // Set the payment details
  $eway = new EwayPaymentLive($eWAY_CustomerID, $eWAY_PaymentMethod, $eWAY_UseLive);

  $eway->setTransactionData("TotalAmount", $txtAmount); //mandatory field
  $eway->setTransactionData("CustomerFirstName", $txtFirstName);
  $eway->setTransactionData("CustomerLastName", $txtLastName);
  $eway->setTransactionData("CustomerEmail", $txtEmail);
  $eway->setTransactionData("CustomerAddress", $txtAddress);
  $eway->setTransactionData("CustomerPostcode", $txtPostcode);
  $eway->setTransactionData("CustomerInvoiceDescription", $txtInvDesc);
  $eway->setTransactionData("CustomerInvoiceRef", $txtInvRef);
  $eway->setTransactionData("CardHoldersName", $txtCCName); //mandatory field
  $eway->setTransactionData("CardNumber", $txtCCNumber); //mandatory field
  $eway->setTransactionData("CardExpiryMonth", $ddlExpiryMonth); //mandatory field
  $eway->setTransactionData("CardExpiryYear", $ddlExpiryYear); //mandatory field
  $eway->setTransactionData("TrxnNumber", $txtInvRef);
  $eway->setTransactionData("Option1", $txtOption1);
  $eway->setTransactionData("Option2", $txtOption2);
  $eway->setTransactionData("Option3", $txtOption3);


  if($eWAY_PaymentMethod =="REAL_TIME_CVN"){ }
	//for REAL_TIME_CVN
	$eway->setTransactionData("CVN", $_POST['txtCSVNumber']);
 

  if($eWAY_PaymentMethod =="GEO_IP_ANTI_FRAUD"){
	//for GEO_IP_ANTI_FRAUD
	$eway->setTransactionData("CustomerIPAddress", $eway->getVisitorIP()); //mandatory field when using Geo-IP Anti-Fraud
	$eway->setTransactionData("CustomerBillingCountry", "AU"); //mandatory field when using Geo-IP Anti-Fraud
  }


  $eway->setCurlPreferences(CURLOPT_SSL_VERIFYPEER, 0); // Require for Windows hosting

  // Send the transaction
  $ewayResponseFields = $eway->doPayment();
 
  if(strtolower($ewayResponseFields["EWAYTRXNSTATUS"])=="false")
  {
     $cartError .= "Transaction Error: " . $ewayResponseFields["EWAYTRXNERROR"] . "\n";
    // foreach($ewayResponseFields as $key => $value)
     //print "\n<br />\$ewayResponseFields[\"$key\"] = $value";
		//UpdateOrderStatus(1, $txtInvRef, $_POST);
		//header("location: ".$_POST['callback']."&force=1");
  }
  else if(strtolower($ewayResponseFields["EWAYTRXNSTATUS"])=="true")
  {
       // payment succesfully sent to gateway
       // Payment succeeded get values returned
       /*$lblResult = " Result: " . $ewayResponseFields["EWAYTRXNSTATUS"] . "<br />";
       $lblResult .= " AuthCode: " . $ewayResponseFields["EWAYAUTHCODE"] . "<br />";
       $lblResult .= " Error: " . $ewayResponseFields["EWAYTRXNERROR"] . "<br />";
       $lblResult .= " eWAYInvoiceRef: " . $ewayResponseFields["EWAYTRXNREFERENCE"] . "<br />";
       $lblResult .= " Amount: " . $ewayResponseFields["EWAYRETURNAMOUNT"] . "<br />";
       $lblResult .= " Txn Number: " . $ewayResponseFields["EWAYTRXNNUMBER"] . "<br />";
       $lblResult .= " Option1: " . $ewayResponseFields["EWAYOPTION1"] . "<br />";
       $lblResult .= " Option2: " . $ewayResponseFields["EWAYOPTION2"] . "<br />";
       $lblResult .= " Option3: " . $ewayResponseFields["EWAYOPTION3"] . "<br />";
       echo $lblResult;*/
		UpdateOrderStatus(5, $txtInvRef, $_POST);
		header("location: ".$_POST['callback']."&force=1");
  }
  else
  {
       // invalid response recieved from server.
       $cartError .= "Error: An invalid response was recieved from the payment gateway. Please check your payment details and try again.";
     
  }

//die();

}

?>